草庐IT

python - __init__ 作为构造函数?

全部标签

go - 使用 initilzer 函数创建的结构不会导出到测试包中吗?

假设我们有一个struct和一个类似这样的结构的构造函数packagemyPackagetypeClientstruct{aTypeAbTypeB}funcNewClient(aTypeA,bTypeB)ConcreteClient{return&Client{a:a,b:b,}}typeConcreteClientinterface{ExportedFunc()}func(c*Client)privateFunc(){//...}func(c*Client)ExportedFunc(){//...}我们在这样的测试包中使用这个客户端var(c=&Client{a:a,b:b,})fu

go - 为什么我从 golang 错误地运行 python 脚本

import("fmt""os/exec""bytes")funcmain(){cmd:="/root/hi.py>/root/1.log"out,err:=exec.Command("python","-c",cmd).Output()fmt.Printf("Out:%s",string(out))fmt.Printf("Err:%s",err.Error())}错误:没有这样的文件错误:/root/hi.py>/root/1.log//hi.py#!/usr/bin/pythonprint('helloworld') 最佳答案

go - 包中的某些函数显示为未定义的 Golang

我正在尝试创建一个Golang应用程序。我的程序包中有一个函数可以很好地使用,即GetCoin函数。但是,我的函数CreateWallet一直给我一个错误,说它没有在包中定义。函数名称是大写的,所以它被导出了,但它就像我的其他文件无法从导入中看到它。这是我要导入的包:https://github.com/pocockn/crypto-compare-go/blob/master/handlers/handlers.go这是我的主要文件。import("github.com/pocockn/crypto-compare-go/handlers""github.com/labstack/e

function - 结构中缺少函数体和字符串标记

这个问题在这里已经有了答案:Functionsignaturewithnofunctionbody(1个回答)关闭4年前。我在Go中发现了一些没有函数体的函数。我知道这意味着Go中的外部函数。但是我在哪里可以找到函数boby呢?typeCreatorfunc(*Beat,*common.Config)(Beater,error)我还在Gostruct中找到了一个字符串。什么意思?typeBeatConfigstruct{//output/publishingrelatedconfigurationsOutputcommon.ConfigNamespace`config:"output"

go - 如果调用的函数来自不同的包,如何同步 goroutine?

为了学习如何在Go中构建Web应用程序,我创建了一个小型Web应用程序,我在其中使用了Gorillamux,我主要在包main、handlers、model、structs下面。我想在浏览文档时使用goroutines我开始知道我需要使用sync包和gofun()。我尝试如下所示在同一个包中使用它并且工作正常。但是,如果调用的函数来自不同的包,如何同步goroutine?//samepackage:workingpackagemodelsimport("fmt""sync")varwgsync.WaitGroupfuncFunc1()(string,error){lexpiry:=Re

go - 如何将变量作为 shell 脚本文件运行

我需要在golang中运行一个变量作为shell脚本文件。我试过像下面的代码packagemainimport("fmt""os/exec")funcmain(){varnamespaceYamlstring=`#!/bin/bashdockerverson`out,err:=exec.Command(namespaceYaml).Output()fmt.Println(err,string(out))}但是我得不到任何结果。我找不到错误在哪里。请任何人解决此问题。提前致谢。 最佳答案 来自官方doc:funcCommand(nam

go - 不能使用函数(类型 func())作为参数类型

packagemainimport("log""strings""asl.com/asl")/*TrivialservicetodemonstratechainingservicetogetherMessagestartsinoriginator,travelsthroughacoupleformatters,andthengetsbacktooriginator*/typeMessageTeststruct{Bodystring`json:"body"`}vars*asl.Servicefuncmain(){var(errerrorcidstring)//varmMessageDel

regex - 从正则表达式构造字符串

我试图找到一种方法来从具有给定interface{}数组的已编译*regexp.Regexp构造string数据.例如:re:=regexp.MustCompile(`(?P\w+)\s*(?P\d+)`)我想通过string和int数据从re中找到的结构构造一个string可以作为接口(interface){}接收。不知道如何在Go中做到这一点。请帮帮我。提前致谢。 最佳答案 这样的库,常被称为Xeger,存在于许多语言中,包括go。然而,这个叫做regen:这是一个从Go/RE2正则表达式生成随机字符串的工具。这是一个例子:$r

unit-testing - 如何测试以确保函数被调用?

我想做单元测试。为了简单起见,我只想确保调用“JWTCheck”。我该怎么做?这是我实现JWTCheck的文件:typeJWTCheckerstruct{SubjectPrefixstring}func(j*JWTChecker)JWTCheck(nexthttp.Handler)http.Handler{//Dosomething}这是我实现路由器的地方:import("net/http""github.com/gorilla/mux")//Routerreturnsapreconfiguredrouterforapplicationfunc(a*Adapter)Router()ht

json - 将接口(interface)传递给函数

我正在尝试编写一个采用json文件名和配置结构的配置包。它应该将json解码到传入的结构中并返回它。我正在尝试使用接口(interface),以便我可以传递任何我想要的结构错误是:panic:接口(interface)转换:interface{}是map[string]interface{},不是*main.ConfigurationData我不太确定如何解决这个问题。这是我的主包packagemainimport("config""commons")typeConfigurationDatastruct{S3ARNstring`json:"S3ARN"`SQSQueueUrlstri